package com.qudian.pay.springboot.starter.log;

import com.qudian.pay.core.aop.BaseInterceptor;
import com.qudian.pay.core.aop.InterceptorChain;
import com.qudian.pay.log.aop.PerformanceDaoInterceptor;
import com.qudian.pay.log.aop.PerformanceServiceInterceptor;
import com.qudian.pay.log.aop.ServiceContextInitInterceptor;
import com.qudian.pay.log.autoproxy.PackageBeanNameAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;

/**
 * Description:
 * 自动配置
 *
 * @Author gaoguangbo
 * @Create 2018-07-12 下午2:37
 */

@Configuration
@EnableConfigurationProperties(LogProperties.class)
public class LogAutoConfigure {

    @Autowired
    private LogProperties logProperties;

    @Bean
    public PackageBeanNameAutoProxyCreator serviceProxyCreator() {
        PackageBeanNameAutoProxyCreator packageBeanNameAutoProxyCreator = new PackageBeanNameAutoProxyCreator();
        packageBeanNameAutoProxyCreator.setInterceptorNames("interceptorChain");
        packageBeanNameAutoProxyCreator.setBasePackage(logProperties.getScanPackage());
        packageBeanNameAutoProxyCreator.setBeanNames(logProperties.getServiceBeanName());
        return packageBeanNameAutoProxyCreator;
    }

    @Bean(name = "interceptorChain")
    public InterceptorChain serviceInterceptorChain() {
        InterceptorChain interceptorChain = new InterceptorChain();
        ArrayList<BaseInterceptor> chains = new ArrayList<>();
        ServiceContextInitInterceptor serviceContextInitInterceptor = new ServiceContextInitInterceptor();
        PerformanceServiceInterceptor performanceServiceInterceptor = new PerformanceServiceInterceptor();
        performanceServiceInterceptor.setThreshold(logProperties.getPerformThreshold());
        chains.add(serviceContextInitInterceptor);
        chains.add(performanceServiceInterceptor);
        interceptorChain.setChains(chains);
        return interceptorChain;
    }

    @Bean
    public PackageBeanNameAutoProxyCreator daoProxyCreator() {
        PackageBeanNameAutoProxyCreator packageBeanNameAutoProxyCreator = new PackageBeanNameAutoProxyCreator();
        packageBeanNameAutoProxyCreator.setInterceptorNames("daoInterceptorChain");
        packageBeanNameAutoProxyCreator.setBasePackage(logProperties.getScanPackage());
        packageBeanNameAutoProxyCreator.setBeanNames(logProperties.getDaoBeanName());
        return packageBeanNameAutoProxyCreator;
    }

    @Bean(name = "daoInterceptorChain")
    public InterceptorChain daoInterceptorChain() {
        InterceptorChain interceptorChain = new InterceptorChain();
        ArrayList<BaseInterceptor> chains = new ArrayList<>();
        PerformanceDaoInterceptor performanceDaoInterceptor = new PerformanceDaoInterceptor();
        chains.add(performanceDaoInterceptor);
        interceptorChain.setChains(chains);
        return interceptorChain;
    }
}
